let util = require('../../utils/util');

let requestDomain = util.config('requestDomain');

let that;

Page({

  data: {
    year: 2017,
    month: 1,
    day: 1,
    nowDate: '',
    maxDate: '',
    orderList: [],
    finding: false
  },

  onLoad: function (options) {
    that = this;
    let date = new Date();
    that.setData({
        year: date.getFullYear(),
        month: date.getMonth() + 1,
        day: date.getDate(),
        nowDate: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate(),
        maxDate: date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate()
    });
    that.getOrderList();
  },

  getOrderList: function() {
    let {year, month, day} = that.data;
    wx.showNavigationBarLoading();
    that.setData({ finding: true });
    wx.request({
        url: requestDomain + 'order/getOrderList',
        method: 'POST',
        data: {
            storeId: util.globalData('storeId'),
            _3rdSession: util.globalData('_3rdSession'),
            year: year,
            month: month,
            day: day
        },
        success: function(result) {
            wx.hideNavigationBarLoading();
            that.setData({ finding: false });
            if(util.checkSuccess(result) && result.data && result.data.data) {
                result = result.data.data;
                let orderList = [];
                for(let orderIndex in result) {
                    let {orderId, timestamp, goodsList} = result[orderIndex];
                    let orderTotalPrice = 0;
                    let date = new Date(timestamp * 1000);
                    let {hours, min, second} = { 
                        hours: date.getHours() < 10 ? '0' + date.getHours() : date.getHours(), 
                        min: date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes(), 
                        second: date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds() 
                    };
                    for(let goodsIndex in goodsList) {
                        let {foodPrice, goodsPrice} = goodsList[goodsIndex];
                        orderTotalPrice += (foodPrice || goodsPrice);
                    }
                    orderList.push({
                        orderId: orderId,
                        orderTime: hours + ':' + min + ':' + second,
                        orderTotalPrice: orderTotalPrice
                    });
                }
                that.setData({ orderList: orderList });
                wx.stopPullDownRefresh();
            }
            else {
                wx.stopPullDownRefresh();
                util.showError('获取失败', '获取订单列表失败' + result.data.errMsg, result.data.errCode);
            }
        },
        fail: function() {
            that.setData({ finding: false });
            wx.stopPullDownRefresh();
            wx.hideNavigationBarLoading();
            util.showError('获取失败', '发起获取订单列表请求失败');
        }
    });
  },

  goOrderDetail: function(e) {
      let orderId = e.currentTarget.dataset.orderid;
      wx.navigateTo({
          url: '/pages/orderDetail/orderDetail?orderId=' + orderId,
      });
  },

  dateChange: function (e) {
      let date = e.detail.value.split('-');
      that.setData({
          year: parseInt(date[0]),
          month: parseInt(date[1]),
          day: parseInt(date[2]),
          nowDate: [parseInt(date[0]), parseInt(date[1]), parseInt(date[2])].join('-')
      });
      that.getOrderList();
  },

  onPullDownRefresh: function() {
      that.getOrderList();
  }

});